//
//  MyTeamController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/12.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "MyTeamController.h"
#import "MyTeamViewCell.h"
#import "MyTeamModel.h"

@interface MyTeamController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *PresonNumb;
@property (weak, nonatomic) IBOutlet UIButton *PersonBtn01;
@property (weak, nonatomic) IBOutlet UIButton *PersonBtn02;
@property (weak, nonatomic) IBOutlet UIButton *PersonBtn03;
@property (weak, nonatomic) IBOutlet UITableView *PersonTable;
@property (strong, nonatomic) NSMutableArray * CellArr;
@property (assign, nonatomic) NSUInteger page;
@property (strong, nonatomic) NSString * type;
@property (strong, nonatomic) MBProgressHUD * hud;


@end

@implementation MyTeamController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"我的团队";
    self.type = @"1";
    
    [self.PersonBtn01 setTitleColor:KRGBColor(123, 123, 123) forState:UIControlStateNormal];
    [self.PersonBtn01 setTitleColor:KRGBColor(251, 42, 67) forState:UIControlStateSelected];
    
    [self.PersonBtn02 setTitleColor:KRGBColor(123, 123, 123) forState:UIControlStateNormal];
    [self.PersonBtn02 setTitleColor:KRGBColor(251, 42, 67) forState:UIControlStateSelected];
    
    [self.PersonBtn03 setTitleColor:KRGBColor(123, 123, 123) forState:UIControlStateNormal];
    [self.PersonBtn03 setTitleColor:KRGBColor(251, 42, 67) forState:UIControlStateSelected];
    
    self.PersonBtn01.selected = YES;
    self.PersonTable.delegate = self;
    self.PersonTable.dataSource = self;
    self.PersonTable.rowHeight = 73;
    self.PersonTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.PersonTable registerNib:[UINib nibWithNibName:@"MyTeamViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.PersonTable.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self.CellArr removeAllObjects];
        [self UpLoadDataOfMyTeamController];
    }];
    
    self.PersonTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        [self UpLoadDataOfMyTeamController];
    }];
    [self.PersonTable.mj_header beginRefreshing];
    
}

- (void)UpLoadDataOfMyTeamController{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [dic setObject:[NSString stringWithFormat:@"%ld",self.page] forKey:@"page"];
    [dic setObject:self.type forKey:@"level"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"index" andAction:@"Team" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"返回的数据信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableDictionary * dic = responseObject[@"data"];
            self.PresonNumb.text = [NSString stringWithFormat:@"%@",dic[@"count"]];
            [self.PersonBtn01 setTitle:[NSString stringWithFormat:@"一级会员 (%@)",dic[@"directCont"]] forState:UIControlStateNormal];
            [self.PersonBtn02 setTitle:[NSString stringWithFormat:@"二级会员 (%@)",dic[@"indirectCount"]] forState:UIControlStateNormal];
            [self.PersonBtn03 setTitle:[NSString stringWithFormat:@"三级会员 (%@)",dic[@"threeCount"]] forState:UIControlStateNormal];
            
            
            NSMutableArray * arr = dic[@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                MyTeamModel * model = [MyTeamModel mj_objectWithKeyValues:dic];
                [self.CellArr addObject:model];
            }
        }
        
        [self.PersonTable.mj_header endRefreshing];
        [self.PersonTable.mj_footer endRefreshing];
        [self.PersonTable reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
        [self.PersonTable.mj_header endRefreshing];
        [self.PersonTable.mj_footer endRefreshing];
        [self.hud hideAnimated:YES];
    }];
    
    
}

- (IBAction)ClickOnBtnOfPersonBtn01:(UIButton *)sender {
    self.type = @"1";
    self.PersonBtn01.selected = YES;
    self.PersonBtn02.selected = NO;
    self.PersonBtn03.selected = NO;
    [self.PersonTable.mj_header beginRefreshing];
    
}
- (IBAction)ClickOnBtnOfPersonBtn02:(UIButton *)sender {
    
    self.type = @"2";
    self.PersonBtn01.selected = NO;
    self.PersonBtn02.selected = YES;
    self.PersonBtn03.selected = NO;
    [self.PersonTable.mj_header beginRefreshing];
}
- (IBAction)ClickOnBtnOfPersonBtn03:(UIButton *)sender {
    
    self.type = @"3";
    self.PersonBtn01.selected = NO;
    self.PersonBtn02.selected = NO;
    self.PersonBtn03.selected = YES;
    [self.PersonTable.mj_header beginRefreshing];
}



-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.CellArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTeamViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MyTeamViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.tag = indexPath.row;
    MyTeamModel * model = self.CellArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (NSMutableArray *)CellArr {
    if (!_CellArr) {
        _CellArr = [NSMutableArray array];
    }
    return _CellArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
